package cn.java.threadmodel.producerconsumer.lockcondition;

import cn.java.threadmodel.producerconsumer.waitnotify.ProducerConsumerQueue;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 小石潭记
 * @date 2021/12/18 10:01
 * @Description: 测试生产消费者模型
 */
public class ProducerConsumerDemo {
    private final static ExecutorService service = Executors.newCachedThreadPool();

    public static void main(String[] args) throws InterruptedException {
        Random random = new Random();
        // 生产者-消费者模型缓冲区
        ProducerConsumerQueue<Integer> queue = new ProducerConsumerQueue<>();
        Producer producer = new Producer(queue);
        Consumer consumer = new Consumer(queue);
        for (int i = 0; i < 3; i++) {
            // 休眠0-50毫秒，增加随机性
            Thread.sleep(random.nextInt(50));
            service.submit(producer);
        }
        for (int i = 0; i < 3; i++) {
            // 休眠0-50毫秒，增加随机性
            Thread.sleep(random.nextInt(50));
            service.submit(consumer);
        }
        // 关闭线程池
        service.shutdown();
    }
}
